import {Injectable} from '@angular/core';
import {RequestUtil} from '../../../util/request-util';
import {Observable} from 'rxjs';
import {MsgResult} from '../../../models/msg-result';
import {FormUtil} from '../../../util/form-util';
import {SiteManage} from './site-manage';
import {SiteV2API} from '../site-v2-constants';
import {SiteDevices} from '../site-devices/site-devices';
import {API} from '../../../constants/constants';

@Injectable()
export class SiteManageService {

  constructor(private requestUtil: RequestUtil,
              private formUtil: FormUtil) {
  }

  insertSite(site: SiteManage): Observable<MsgResult> {
    let params = this.formUtil.serialize(site);
    return this.requestUtil.post(SiteV2API.insertSite, params);
  }

  deleteSite(id: string,siteCode :string): Observable<MsgResult> {
    let params = 'siteCode=' + siteCode;
    return this.requestUtil.delete(SiteV2API.deleteSite.replace('{id}', id), params);
  }

  delSites(ids: Array<string>,siteCodes:Array<string>) {
    let params = 'idStr=' + ids + '&siteCodeStr=' + siteCodes;
    console.log(params);
    return this.requestUtil.post(SiteV2API.delSites, params);
  }

  getSiteById(id: string) {
    return this.requestUtil.get(SiteV2API.getSiteById.replace('{id}', id))
  }

  updateSite(site: SiteManage): Observable<MsgResult> {
    let params = this.formUtil.serialize(site);
    return this.requestUtil.put(SiteV2API.updateSite.replace('{id}', site.id),params);
  }

  /**
   * 获取行政区划关联树
   * @param userCode
   * @returns {Observable<MsgResult>|any}
   */
  getRetrieveRegionsTree(regionCode: string): Observable<MsgResult> {
    let params = 'regionCode=' + regionCode;
    return this.requestUtil.get(SiteV2API.getRetrieveRegionsTree, params);
  }

  getSiteCategoryInfos(): Observable<MsgResult> {
    let params = 'isUse=' + '1';
    return this.requestUtil.get(SiteV2API.getAllSiteCate, params);
  }

  /**
   *
   * @returns {Observable<MsgResult>|any}
   */
  getGovInfos(param:string): Observable<MsgResult>{
    let params = 'name=' + param;
    return this.requestUtil.get(SiteV2API.getGovInfos,params);
  }

  /**
   *
   * @returns {Observable<MsgResult>|any}
   */
  getEntInfos(param:string): Observable<MsgResult>{
    let params = 'name=' + param;
    return this.requestUtil.get(SiteV2API.getEntInfos,params);
  }
  // 修改站点设备
  siteMonitorEdit(siteDevice):Observable<MsgResult>{
    let params = this.formUtil.serialize(siteDevice);
    return this.requestUtil.put(SiteV2API.editSiteDevice.replace('{id}', siteDevice.id),params);
  }
  /**
   * 删除站点区间
   * @param id :站点区间id
   * @returns {Observable<MsgResult>|any}
   */
  deleteSiteRange(id): Observable<MsgResult> {
    return this.requestUtil.delete(SiteV2API.deleteSiteRangeById.replace('{id}', id));
  }
  /**
   * 通过ID获取移动设备
   * @param id :移动设备
   * @returns {Observable<MsgResult>|any}
   */
  getSiteRangeById(id): Observable<MsgResult> {
    return this.requestUtil.get(SiteV2API.getSiteRangeById.replace('{id}', id));
  }
  /**
   * 增加站点区间
   * @param mobileDev: 站点区间数据
   * @returns {Observable<MsgResult>|any}
   */
  postSiteRange(mobileDev): Observable<MsgResult> {
    let params = this.formUtil.serialize(Object.assign(JSON.parse(JSON.stringify(mobileDev)), {startDate: null, endDate: null}));
    params += '&startDate=' + mobileDev.startDate + '&endDate=' + mobileDev.endDate;
    return this.requestUtil.post(SiteV2API.postSiteRange, params);
  }
  /**
   * 修改站点区间
   * @param mobileDev: 站点区间数据
   * @returns {Observable<MsgResult>|any}
   */
  updateSiteRange(mobileDev): Observable<MsgResult> {
    let params = this.formUtil.serialize(Object.assign(JSON.parse(JSON.stringify(mobileDev)), {startDate: null, endDate: null, id: null}));
    params += '&startDate=' + mobileDev.startDate + '&endDate=' + mobileDev.endDate;
    return this.requestUtil.put(SiteV2API.putSiteRangeById.replace('{id}', mobileDev.id), params);
  }
  /**
   * 获取设备类型
   * @returns {Observable<MsgResult>|any}
   */
  getDeviceType(): Observable<MsgResult> {
    return this.requestUtil.get(SiteV2API.insertDeviceCate);
  }

  /**
   * 获取父级行政区划
   */
    getParentRegion(regionCode): Observable<MsgResult> {
    //    &regionCode=410105000&regionType=10
      const params = 'regionCode=' + regionCode + '&regionLevel=1';
      return this.requestUtil.get(SiteV2API.getParentRagion, params)
    }
  /**
   * 获取父级行政区划
   */
  postIdsForSite(siteCode, idStr): Observable<MsgResult> {
    const params = 'siteCode=' + siteCode + '&idStr=' + idStr;
    return this.requestUtil.get(SiteV2API.postIdsForSite, params)
  }
  /**
   * 获取父级行政区划
   */
  getHistoryAddr(siteCode): Observable<MsgResult> {
    const params = 'siteCode=' + siteCode;
    return this.requestUtil.get(SiteV2API.historySiteRangeAddr, params)
  }
}
